import logging
import logging.handlers
import os

BaseDir = os.path.dirname(__file__)


def init_logging():
    # Create logger
    logger = logging.getLogger()
    # Set log level
    logger.setLevel(logging.DEBUG)

    # Create file handler
    fh = logging.handlers.TimedRotatingFileHandler(
        BaseDir + "/log/log.log",
        when="midnight",
        interval=1,
        backupCount=7
    )

    # Create stream handler
    sh = logging.StreamHandler()

    # Set handler levels
    fh.setLevel(logging.INFO)
    sh.setLevel(logging.INFO)

    # Create formatter
    fmt = "%(asctime)s %(levelname)s [%(name)s] " \
          "[%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
    formatter = logging.Formatter(fmt=fmt)

    # Add formatter to handlers
    fh.setFormatter(formatter)
    sh.setFormatter(formatter)

    # Add handlers to logger
    logger.addHandler(sh)
    logger.addHandler(fh)